/**
 * Created by Administrator on 2014/12/24.
 */

package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.Schedule;
import com.smartrobot.smartrobot.util.User;

import java.util.ArrayList;
import java.util.List;


public class DetailListViewAdapter extends BaseAdapter {

    private Context context;
    private List<Schedule> scheduleList;



    public DetailListViewAdapter(Context context){

        this.context = context;
        this.scheduleList = new ArrayList<Schedule>();
//        this.msgList.add(new Schedule("1","9:00","小红约了我"));
//        this.msgList.add(new Schedule("1","10:00","小绿约了我"));
//        this.msgList.add(new Schedule("1","11:00","小黄约了我"));
//        this.msgList.add(new Schedule("1","12:00","小轻约了我"));
//        this.msgList.add(new Schedule("1","13:00","小蓝约了我"));
//        this.msgList.add(new Schedule("1","14:00","小紫约了我"));
//        this.msgList.add(new Schedule("1","15:00","小a约了我"));
//        this.msgList.add(new Schedule("1","16:00","小b约了我"));
//        this.msgList.add(new Schedule("1","17:00","小c约了我"));
//        this.msgList.add(new Schedule("1","18:00","小d约了我"));
//        this.msgList.add(new Schedule("1","19:00","小e约了我"));
//        this.msgList.add(new Schedule("1","20:00","小f约了我"));



    }



    @Override
    public int getCount() {
       return scheduleList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        try{
            ViewHolder holder;
            if(view == null){
                //新建一个holder
                view =  LayoutInflater.from(context).inflate(R.layout.page_schedule_adapter_item, viewGroup, false);
                holder = new ViewHolder();

                holder.time = (TextView)view.findViewById(R.id.detail_adapter_item_time);
                holder.event = (TextView)view.findViewById(R.id.detail_adapter_item_event);

                view.setTag(holder);

            }else{
                holder = (ViewHolder)view.getTag();
            }

            Schedule schedule = scheduleList.get(position);
            holder.time.setText(schedule.getTime().substring(0,2)+":"+schedule.getTime().substring(2,4));
            holder.event.setText(schedule.getEvent());


        }catch (Exception e){
            LogToSd.error(e.getMessage());
        }
        return view;
    }


    public void notiDataChange(Schedule schedule){

        this.scheduleList.add(schedule);
        this.notifyDataSetChanged();

    }

    public void notiDataChangeList(List<Schedule> list){

        this.scheduleList.clear();
        this.scheduleList.addAll(list);
        this.notifyDataSetChanged();

    }


    public void saveFile(String fileName){

        try {
            if(FileUtil.createFile(fileName))
            {
                FileUtil.writeScheduleToFile(scheduleList,fileName);
                Log.d("--------->", "保存成功"+scheduleList.size());
                LogToSd.debug("日程保存文件,保存成功!");
            }
            else
            {
                Log.e("--------->","无法创建文件");
                LogToSd.error("日程保存文件,错误"+"无法创建文件夹");
            }
        } catch (Exception e) {
            Log.e("--------->","无法找到文件");
            LogToSd.error("日程保存文件,错误："+e.getMessage().toString());
        }

    }


    class ViewHolder{
        TextView time;
        TextView event;
    }


}
